package learn;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    LinkedList<Integer> path = new LinkedList<Integer>();

    public List<List<Integer>> combine(int n, int k) {
        combineHelper(n, k, 1);
        return result;
    }

    /**
     * 递归组合问题
     * @param n
     * @param k
     * @param startIndex
     */
    private void combineHelper(int n, int k, int startIndex) {
        if (path.size() == k) {
            result.add(new ArrayList<Integer>(path));
            return;
        }

        for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            combineHelper(n, k, i+1);
            path.removeLast();
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.combine(4, 2);
        System.out.println(JSONObject.toJSON(solution.result));
    }

}
